﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cotizav.CONEXION;
using System.Transactions;
using System.Windows.Forms;

namespace Cotizav
{
    public class CtrlCategoria
    {
        private ConexionDataContext conexion;
        
        internal List<CONEXION.CATEGORIA> getListCategoria()
        {
            using (conexion = new ConexionDataContext())
            {
                try
                {
                    var query = conexion.CATEGORIA.Where(x => x.eliminado == 0).ToList();
                    return query;
                }
                catch (Exception)
                {                    
                    throw;
                }   
            }
        }

        internal System.Windows.Forms.DialogResult registrarCategoria(CATEGORIA cat)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        CONEXION.CATEGORIA categoria = cat;
                        DialogResult result = DialogResult.Cancel;

                        //insertamos el id
                        int correlativoCategoria = conexion.COTIZAV_CORRELATIVO.Single(x => x.IDENTIFICADOR == "COTIZAV").CATEGORIA.Value;
                        string num = Convert.ToString(correlativoCategoria);
                        string numero = num.Trim().PadLeft(10, '0');
                        string idCategoria= "CATE_" + numero;

                        cat.idCategoria = idCategoria;

                        //insertamos el sector
                        conexion.CATEGORIA.InsertOnSubmit(cat);
                        conexion.SubmitChanges();
                        conexion.increm_ContCategoria();

                        transactionScope.Complete();
                        result = DialogResult.OK;

                        return result;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        internal System.Windows.Forms.DialogResult actualizarCategoria(CATEGORIA cat)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        CONEXION.CATEGORIA sect = conexion.CATEGORIA.Single(x => x.idCategoria == cat.idCategoria);
                        DialogResult result = DialogResult.Cancel;
                        //actualizamos
                        sect.nombreCategoria = cat.nombreCategoria;
                        sect.eliminado = cat.eliminado;

                        conexion.SubmitChanges();
                        transactionScope.Complete();
                        result = DialogResult.OK;

                        return result;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        internal void eliminarCategoria(CATEGORIA cat)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        CONEXION.CATEGORIA cate = conexion.CATEGORIA.Single(x => x.idCategoria == cat.idCategoria);
                        //actualizamos
                        cate.eliminado = cat.eliminado;

                        conexion.SubmitChanges();
                        transactionScope.Complete();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }
    }
}
